<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>文件读取</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html, body{
            height: 100%;
        }
        body{
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
            align-items: center;
            padding-top: 20px;
        }
        #container{
            width: 200px;
            height: 200px;
            border: 1.6px dashed #999999b9;
            background: #f6f6f6;
            font-family: 微软雅黑;
            color: #999999;
            position: relative;
            
        }
        #container .bg{
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: flex-end;
            align-items: center;
            box-sizing: border-box;
            
            position: absolute;
            top: 0;
            
        }
        .icon{
            width: 80px;
            height: 80px;
            background: url() center no-repeat;
            background-size: contain;
            opacity: 0.5;
        }
        p{
            width: 100%;
            height: 55px;
            margin-top: 15px;
            text-align: center;
            line-height: 55px;
            letter-spacing: 1.3px;/*
            background: #cacaca8a;*/
        }
        .p1{
            
        }
        .p2{
            display: none;
            color: #ff1414;
            background: #cacaca8c;
            z-index: 100;
            cursor: pointer;
        }
        input{
            width: 100%;
            height: 100%;
            opacity: 0;
            position: absolute;
            z-index: 99;
            cursor: pointer;
        }
        #container:hover {
            background: rgba(99, 99, 99, 0.107);
        }
        .p2:hover{
            background: #2f2f2f54;
        }




        button{
            width: 200px;
            height: 40px;
            margin-top: 20px;
            border: none;
            border-radius: 5px;
            background-color: #4099ff;
            color: #fff;
            letter-spacing: 1.5px;
            font-size: 18px;
        }
        button.do{
            display: none;
        }
        button.cancle{
            border: 2px solid rgb(255, 62, 62);
            background-color: rgba(255, 255, 255, 0);
            color: rgb(255, 62, 62);
        }


        #lastImg{
            margin-top: 30px;
            width: fit-content;
            height: fit-content;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        #lastImg span{
            
            border-radius: 15px;
            display: inline-block;
            width: 100px;
            height: 25px;
            line-height: 25px;
            font-size: 14px;
            text-align: center;
            background: #4099ff;
            color: #fff;

            margin-bottom: 15px;
        }
        .currentImg{
            width: 280px;
            height: 280px;
            border-radius: 50%;
            box-shadow: 10px 10px 10px rgba(130, 130, 130, 0.425);
        }
    </style>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
    <p id="hiddenP" style="display: none;"></p>
    <div id="container">
        <input id="file1" type="file" name="" >
        <div class="bg">
            <div class="icon"></div>
            <p class="p1">上传照片</p>
            <p class="p2">更换</p>
        </div>
        
    </div>

    
    <button class="do" onclick="handleUpload()">确定</button>
    <button class="cancle" onclick="history.go(-1)">返回</button>

    <div id="lastImg">
        <span>当前头像</span>
        <div class="currentImg"></div>
    </div>
    <script>
        
        const inputFile = document.getElementById("file1")
        file1.onchange=()=>{
            const file = inputFile.files[0]
            //console.log(file)
            var kb = (file.size / 1024) //
            //console.log(file.size/1024)
            //console.log((file.size/1024/1000).toFixed(2,0)+"mb")
            let mb = (kb/1000).toFixed(2,0)
            console.log(kb+"kb ，"+mb+"mb")
            if(kb > 1000){ //  >2mb
                Alert(kb)
            }


            let reader = new FileReader()
            reader.readAsDataURL(file)
            reader.onload=()=>{
                base64 = reader.result
                const content = base64.replace(/^data:(.*?);base64,/, '');
                content1 = content
                const type = base64.match(/^data:(.*?);base64,/)[1];
                console.log(type)
                console.log(content)
                
                document.querySelector("#container").style.background='url('+base64+') no-repeat'
                document.querySelector("#container").style.backgroundSize='cover'
                document.querySelector("#container").style.backgroundPosition='center'
                document.querySelector("#container .bg .icon").style.background='none'
                document.querySelector("#container .bg .p1").style.display='none'
                document.querySelector("#container .bg .p2").style.display='block'
                
                $('.do').show()
                
            }


            document.getElementsByClassName("p2")[0].addEventListener('click', ()=>{
                $(".do").hide()
                document.getElementById("file1").files[0] = null
                document.querySelector("#container").style.background='#f6f6f6'
                document.querySelector("#container .bg .icon").style.background='url() center no-repeat'
                document.querySelector("#container .bg .icon").style.backgroundSize='contain'
                document.querySelector("#container .bg .p1").style.display='block'
                document.querySelector("#container .bg .p2").style.display='none'

            })


            

        }



        function handleUpload() {
            $('.do').text('上传中...')
                
                var msg = {
                    user_id: localStorage.getItem('XGB-logon'),
                    imgBase64: content1
                }
                
                axios.post('https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/uploadUserImg', msg)
                  .then(res=>{
                    console.log("接收到axios响应")
                    //console.log(res.data)
                    localStorage.setItem('XGStudio_userImg', res.data.fileID)
                    window.location.reload()
                  })
                  .catch(e=>{})










            }


    </script>

    <script>
        var currentImgUrl = localStorage.getItem('XGStudio_userImg')
        if(currentImgUrl===null){
            $('#lastImg').hide()
        }else{
            $('.currentImg').css({
                background: `url(${currentImgUrl}) no-repeat`,
                backgroundPosition: 'center',
                backgroundSize: 'cover'
            })
        }
        
    </script>

    <script>
        var Alert =(i)=>{
            var zhezhao = document.createElement('div')
            
            zhezhao.style.position='fixed'
            zhezhao.style.top='0'
            zhezhao.style.zIndex='999'
            zhezhao.style.width='100%'
            zhezhao.style.height='100%'
            zhezhao.style.backgroundColor='#0000007d'
            zhezhao.style.display='flex'
            zhezhao.style.justifyContent='center'
            zhezhao.style.alignItems='center'
            zhezhao.onclick=function(){
                zhezhao.style.display='none'
                $('.p2').click()
            }
        
            var box = document.createElement('div')
            box.style.width='70%'
            box.style.boxSizing='border-box'
            box.style.padding='20px'
            box.style.backgroundColor='#fff'
            box.style.borderRadius='.2415rem'
            box.className='Alert'
            let mb = (i/1000).toFixed(2,0)
            var boxHTML = `当前文件大小为&nbsp;<span style="color:red;">${mb}MB&nbsp;</span>，无法上传，建议更换小于1MB的图片！`
            
        
            box.innerHTML=boxHTML
            
        
        
            zhezhao.appendChild(box)
            document.body.appendChild(zhezhao)
        
        }
    </script>
    
</body>
</html>